package cn.hbwe.controller;


import cn.hbwe.Dto.dLoginDTO;
import cn.hbwe.Dto.dRegisterDTO;
import cn.hbwe.pojo.dto.DoctorDto;
import cn.hbwe.pojo.entity.Doctor;
import cn.hbwe.pojo.vo.DoctorVo;
import cn.hbwe.service.IDoctorService;
import cn.hbwe.util.PageVO;
import cn.hbwe.util.Result;
import cn.hbwe.util.constant.BaseContext;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */
@RestController
@RequestMapping("/doctor")
@Api(tags = "医生类接口")
@CrossOrigin
public class DoctorController {
    @Autowired
    private IDoctorService doctorService;

    /**
     * 医生分页查询
     * @param currentPage
     * @param pageSize
     * @param
     * @return
     */
    /*@ApiOperation("医生分页查询")
    @GetMapping("/doctor/{currentPage}/{pageSize}")
    public Result<PageVO<DoctorVo>> getDoctorVo(@PathVariable Long currentPage, @PathVariable Long pageSize, String name) {
        PageVO<DoctorVo> page = doctorService.getDoctorVo(currentPage,pageSize,name);
        return Result.success(page);
    }*/
    @ApiOperation("根据科室ID查询医生信息并进行分页查询")
    @GetMapping("/byDepartmentId/{currentPage}/{pageSize}")
    public Result<PageVO<DoctorVo>> getByDepartmentId(@PathVariable Long currentPage, @PathVariable Long pageSize, Integer id) {
        PageVO<DoctorVo> doctors = doctorService.getByDepartmentId(currentPage,pageSize,id);
        return Result.success(doctors);
    }
    /**
     * 根据科室id查询医生信息
     * @param id
     * @return
     */
    /*@ApiOperation("根据科室id查询医生信息")
    @GetMapping("/getByDepartmentId/{id}")
    public Result<List<Doctor>> getByDepartmentId(@PathVariable Integer id) {
        List<Doctor> doctors = doctorService.getByDepartmentId(id);
        return Result.success(doctors);
    }*/

    /**
     * 添加医生信息
     * @param doctorDto
     * @return
     */
    @ApiOperation("添加医生")
    @PostMapping("/save")
    public Result<String> saveDoctor(@RequestBody DoctorDto doctorDto) {
        doctorService.saveDoctor(doctorDto);
        return Result.success("添加医生信息成功");
    }

    /**
     * 修改医生信息
     * @param doctorDto
     * @return
     */
    @PutMapping("/modify")
    @ApiOperation("修改医生信息")
    public Result<String> modifyDoctor(@RequestBody DoctorDto doctorDto) {
        doctorService.updateDoctor(doctorDto);
        return Result.success("医生信息修改成功");
    }

    /**
     * 根据id删除医生信息
     * @param id
     * @return
     */
    @ApiOperation("根据id删除医生信息")
    @DeleteMapping("/delete/{id}")
    public Result<String> removeDoctor(@PathVariable Integer id) {
        doctorService.removeById(id);
        return Result.success("医生信息删除成功");
    }

    /**
     * 用户登录
     * @param dloginDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/dlogin")
    @ApiOperation("医生登录")
    public Result<Doctor> login(@RequestBody @Valid dLoginDTO dloginDTO, BindingResult bindingResult) {
        List<ObjectError> errors = bindingResult.getAllErrors();
        for (ObjectError error : errors) {
            return Result.error(error.getDefaultMessage());
        }
        Doctor doctor = doctorService.dlogin(dloginDTO);
        Integer id = doctor.getDoctorId();
        BaseContext.setCurrentId(id);
        return Result.success(doctor);
    }

    //--------------------------------------------------------------------------------------------------------------




    /**
     * 医生注册（管理员使用）
     * @param dregisterDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/dregister")
    @ApiOperation("医生注册")
    public Result<Doctor> register(@RequestBody @Valid dRegisterDTO dregisterDTO, BindingResult bindingResult) {
        List<ObjectError> errors = bindingResult.getAllErrors();
        for (ObjectError error : errors) {
            return Result.error(error.getDefaultMessage());
        }
        Doctor doctor = doctorService.dregister(dregisterDTO);
        return Result.success(doctor);
    }


    @ApiOperation("根据医生姓名查询医生信息")
    @GetMapping("/select/{doctorName}")
    public Result<List<Doctor>> getByDoctor(@PathVariable String doctorName){
        List<Doctor> doctorList=doctorService.getByDoctorName(doctorName);
        return Result.success(doctorList);
    }


    //------yuhangyu------------------------------------------------------------------------------------------
    //医生端
    //查看自己的信息
    @Autowired
    private IDoctorService iDoctorService;
    @GetMapping("/selectByOneself/{doctorId}")
    public Doctor selectByOneself(@PathVariable Integer doctorId){
        return  iDoctorService.selectByOneself(doctorId);
    }







    @GetMapping("/getDname/{did}")
    public Result getPname(@PathVariable Integer did) {
        String dname=iDoctorService.dname(did);
        System.out.println(dname);
        return Result.success(dname);
    }







}
